package juc.cas;

import java.util.concurrent.atomic.AtomicMarkableReference;

/*
* AtomicStampedReference 可以看到各个值变化的过程|版本迭代过程
* AtomicMarkableReference 只关心值有没有被修改过
* */
public class Test9 {
    public static void main(String[] args) throws InterruptedException {
        AtomicMarkableReference<GarbageBag>garbageBagReference=new AtomicMarkableReference<>(new GarbageBag("满垃圾"),true);
        GarbageBag bag = garbageBagReference.getReference();
        System.out.println("下班了想换垃圾袋");

        new Thread(()->{
            System.out.println("保姆倒垃圾");
            bag.setState("垃圾空");
            garbageBagReference.compareAndSet(bag,bag,true,false);
        },"保姆").start();
        Thread.sleep(100);
        System.out.println("确定要换？"+ garbageBagReference.compareAndSet(bag, new GarbageBag("垃圾空"), true, false));
        System.out.println(garbageBagReference.getReference());
    }
}
class GarbageBag{
    private String state;

    public void setState(String state) {
        this.state = state;
    }

    public GarbageBag(String state) {
        this.state = state;
    }
    @Override
    public String toString() {
        return "GarbageBag{" +
                "state='" + state + '\'' +
                '}';
    }
}
